package com.wwwarehouse.taskcenter.fragment.job_point.step7;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.adapter.job_point.RuleAdapter;
import com.wwwarehouse.taskcenter.bean.job_point.JobPointSetRuleResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.wwwarehouse.taskcenter.constant.job_point.TaskCenterCommon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_BUSINESS_ID;

/**
 * Created by Jackie on 2017/11/13.
 * 选择作业点规则  详情
 */

public class ChooseJobPointRuleDetailsFragment extends BaseFragment {
    private View mRootView;
    private StateLayout mStateLayout;
    private ListView mListView;
    private RuleAdapter mAdapter;

    private ArrayList<JobPointSetRuleResponseBean.JobPointRuleDetailsBean> mJobPointRuleDetailsList;

    public static final String KEY_TEMP_INSTANCE_ID = "key_temp_instance_id";

    private static final int REQUEST_GET_RULE_INFO = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_choose_job_point_rule_details, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initEvent();
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        if (enter) {
            if (TaskCenterCommon.getInstance().isDetails()) {
                initData();
            } else {
                getRuleInfo();
            }
        }

        return null;
    }

    private void initView() {
        mStateLayout = findView(mRootView, R.id.sl_layout);
        mListView = findView(mRootView, R.id.lv_all);
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            final int page = bundle.getInt("page");
            mJobPointRuleDetailsList = bundle.getParcelableArrayList("dataList");

            mListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                    int itemHeight = mListView.getMeasuredHeight() / 10;

                    int start;
                    int end;

                    if ((page - 1) * 10 + 10 < mJobPointRuleDetailsList.size()) {
                        start = (page - 1) * 10;
                        end = (page - 1) * 10 + 10;
                    } else {
                        start = (page - 1) * 10;
                        end = mJobPointRuleDetailsList.size();
                    }

                    mAdapter = new RuleAdapter(mActivity, R.layout.item_job_point_rule_details, mJobPointRuleDetailsList.subList(start, end), itemHeight);

                    mListView.setAdapter(mAdapter);

                    mAdapter.setOnDetailsClickListener(new RuleAdapter.OnDetailsClickListener() {
                        @Override
                        public void onDetailsClick(JobPointSetRuleResponseBean.JobPointRuleDetailsBean jobPointRuleDetailsBean) {
                            //作业点规则详情
                            Bundle bundle = new Bundle();
                            bundle.putString(KEY_TEMP_INSTANCE_ID, jobPointRuleDetailsBean.getTempInstanceId());

                            ChooseJobPointItemDetailsFragment chooseJobPointItemDetailsFragment = new ChooseJobPointItemDetailsFragment();
                            chooseJobPointItemDetailsFragment.setArguments(bundle);
                            pushFragment(chooseJobPointItemDetailsFragment);
                        }
                    });
                }
            });
        }
    }

    private void initEvent() {

    }


    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {
    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_GET_RULE_INFO:
                    if ("0".equals(commonClass.getCode())) {
                        JobPointSetRuleResponseBean jobPointSetRuleResponseBean = JSON.parseObject(commonClass.getData().toString(), JobPointSetRuleResponseBean.class);
                        mJobPointRuleDetailsList = (ArrayList<JobPointSetRuleResponseBean.JobPointRuleDetailsBean>) jobPointSetRuleResponseBean.getList();

                        mListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                                mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                                int itemHeight = mListView.getMeasuredHeight() / 10;

                                mAdapter = new RuleAdapter(mActivity, R.layout.item_job_point_rule_details, mJobPointRuleDetailsList, itemHeight);

                                mListView.setAdapter(mAdapter);

                                mAdapter.setOnDetailsClickListener(new RuleAdapter.OnDetailsClickListener() {
                                    @Override
                                    public void onDetailsClick(JobPointSetRuleResponseBean.JobPointRuleDetailsBean jobPointRuleDetailsBean) {
                                        Bundle bundle = new Bundle();
                                        bundle.putString(KEY_TEMP_INSTANCE_ID, jobPointRuleDetailsBean.getTempInstanceId());

                                        ChooseJobPointItemDetailsFragment chooseJobPointItemDetailsFragment = new ChooseJobPointItemDetailsFragment();
                                        chooseJobPointItemDetailsFragment.setArguments(bundle);
                                        pushFragment(chooseJobPointItemDetailsFragment);
                                    }
                                });
                            }
                        });
                    } else {
                        mStateLayout.showSystemView(commonClass.getMsg(), false);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mStateLayout.showProgressView(false);

                               getRuleInfo();
                            }
                        });
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(false);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mStateLayout.showProgressView(false);

                    getRuleInfo();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    /**
     * 查询已设置作业点规则
     */
    private void getRuleInfo() {
        mStateLayout.showProgressView(false);

        Bundle bundle = getArguments();

        if (bundle != null) {
            String businessId = bundle.getString(KEY_BUSINESS_ID);
            int page = bundle.getInt("page");

            Map map = new HashMap();

            map.put("buId", businessId);
            map.put("page", page);
            map.put("size", 10);

            NoHttpUtils.httpPost(TaskCenterConstant.URL_GET_RULE_INFO, map, mOnResponseListener, REQUEST_GET_RULE_INFO);
        }
    }
}
